﻿/****************************** Module Header ******************************\
 * Module Name:  mxComboBox.cs
 * Module Version:  1.0.0 (2012-8-25)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension winformcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace UCL.Forms
{
    /// <summary>
    /// ComboBox扩展方法
    /// </summary>
    public static class mxComboBox
    {
        /// <summary>
        /// 使控件不可编辑
        /// </summary>
        /// <param name="c"></param>
        public static void Lock(this ComboBox c)
        {
            c.Enabled = false;
            Label lbMark = new Label() { Parent = c.Parent, Width = c.Width - 23, Height = c.Height - 6, Left = c.Left + 3, Top = c.Top + 3, Text = c.Text, BackColor = Color.White };
            lbMark.BringToFront();
            c.Tag = lbMark;
        }

        /// <summary>
        /// 使控件可编辑
        /// </summary>
        /// <param name="c"></param>
        public static void UnLock(this ComboBox c)
        {
            c.Enabled = true;
            if (c.Tag != null) ((Label)c.Tag).Visible = false;
        }

        /// <summary>
        /// 设置ComboBox控件的行高
        /// </summary>
        /// <param name="c"></param>
        /// <param name="height"></param>
        /// <param name="colName"></param>
        public static void SetLineHeight(this ComboBox c, int height = 16,string colName="Name")
        {
            int varh = (height - 12) / 2;
            c.Height = 12;
            c.ItemHeight = height;
            c.DrawMode = DrawMode.OwnerDrawVariable;
            c.DrawItem += (object sender, DrawItemEventArgs e) =>
            {
                if (e.Index < 0) return;
                e.DrawBackground();
                e.DrawFocusRectangle();
                object v = c.Items[e.Index];
                if (v.GetType() == typeof(DataRow) || v.GetType() == typeof(DataRowView))
                {
                    v = ((DataRowView)v)[colName];
                }
                e.Graphics.DrawString(v.ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + varh);
            };
        }

        /// <summary>
        /// 设置ListBox控件的行高 2013-6-25 by sutroon
        /// </summary>
        /// <param name="c"></param>
        /// <param name="height"></param>
        public static void SetLineHeight(this ListBox c, int height = 16, string colName = "Name")
        {
            int varh = (height - 12) / 2;
            c.DrawMode = DrawMode.OwnerDrawVariable;
            c.DrawItem += (object sender, DrawItemEventArgs e) =>
            {
                if (e.Index < 0) return;
                e.DrawBackground();
                e.DrawFocusRectangle();
                object v = c.Items[e.Index];
                if (v.GetType() == typeof(DataRow) || v.GetType() == typeof(DataRowView))
                {
                    v = ((DataRowView)v)[colName];
                }
                e.Graphics.DrawString(v.ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + varh);
            };
            c.MeasureItem += (object sender, MeasureItemEventArgs e) =>
            {
                e.ItemHeight = height;
            };
             
        }
    }
}
